package com.youzan.cloud.component.biz.member.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.youzan.cloud.component.biz.member.common.enums.LevelEnum;
import com.youzan.cloud.component.biz.member.common.util.OutResultUtil;
import com.youzan.cloud.component.biz.member.dal.dataobject.MemberRelationship;
import com.youzan.cloud.component.biz.member.dal.dataobject.MerchantConfiguration;
import com.youzan.cloud.component.biz.member.dal.mapper.MemberRelationshipMapper;
import com.youzan.cloud.component.biz.member.dal.mapper.MerchantConfigurationMapper;
import com.youzan.cloud.component.biz.member.dto.*;
import com.youzan.cloud.component.biz.member.youzanopen.api.YzMemberApiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
/**
 * @author jinzetao
 * @version 1.0
 * @description: 商家主动推送会员等级变化
 * @date 2021/8/4 15:48
 */
@Service
@Slf4j
public class MemberGradeBusinessService extends AbstractService{
    @Autowired
    private MemberRelationshipMapper memberRelationshipMapper;
    @Autowired
    private MerchantConfigurationMapper merchantConfigurationMapper;
    @Autowired
    private YzMemberApiService yzMemberApiService;
    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;
    @Override
    public LogDTO buildLog(Object inputParam) {
        return null;
    }

    @Override
    public BaseResult handleException(Exception e) {
        return OutResultUtil.fail("组件异常!","500",e);
    }

    @Override
    public BaseResult doBiz(MemberBaseDto memberBaseDto) {
        MemberGradeBusinessInputDto memberGradeBusinessInputDto = (MemberGradeBusinessInputDto) memberBaseDto.getMemberGradeDto();
        String thirdAccount = memberGradeBusinessInputDto.getThirdAccount();
        //查询该用户的yzopenid
        MemberQueryBusinessInputDto memberQueryBusinessInputDto = new MemberQueryBusinessInputDto();
        memberQueryBusinessInputDto.setThirdAccount(thirdAccount);
        memberQueryBusinessInputDto.setThirdAccountType(memberGradeBusinessInputDto.getThirdAccountType()==null?"1":memberGradeBusinessInputDto.getThirdAccountType());
        MemberRelationship memberRelationship = memberRelationshipMapper.selectByAccountAndAccountType(memberQueryBusinessInputDto);
        MerchantConfiguration merchantConfiguration = merchantConfigurationMapper.selectAll(LevelEnum.LEVEL_MAPPING);
        JSONObject jsonObject = JSON.parseObject(merchantConfiguration.getConfigValue());
        String levelAlias = jsonObject.getString(memberGradeBusinessInputDto.getCno());
        if(memberRelationship != null){
            threadPoolTaskExecutor.execute(()->
                yzMemberApiService.initUserGrade(levelAlias,memberRelationship.getYzOpenid(), memberRelationship.getKdtid().toString()));
        }
        return OutResultUtil.success("等级更新成功");
    }
}
